package com.example.administrator.familfinalcialmanagement.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.Info;
import com.example.administrator.familfinalcialmanagement.bean.ReasionBean;
import com.example.administrator.familfinalcialmanagement.databinding.FragmentIndexBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.ReasionBeanDao;
import com.example.administrator.familfinalcialmanagement.ui.activity.MyInfoActivity;
import com.example.administrator.familfinalcialmanagement.ui.adapter.ChildClassNameRightAdapter;
import com.example.administrator.familfinalcialmanagement.ui.adapter.FuncationAdapter;
import com.example.administrator.familfinalcialmanagement.ui.adapter.ParentClassNameRightAdapter;
import com.example.administrator.familfinalcialmanagement.ui.view.CycleViewPager;
import com.example.administrator.familfinalcialmanagement.ui.view.TaoLinear;
import com.example.administrator.familfinalcialmanagement.util.GlideUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.listener.OnChildListener;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/26.
 */

public class IndexFragment extends BaseLazyFragment {

    FragmentIndexBinding binding;
    List<Info> mList = new ArrayList<>();

    List<String> rightStrs = new ArrayList<>();
    FuncationAdapter rightAdapter;

    List<String> secondStrings = new ArrayList<>();
    ChildClassNameRightAdapter adapter;

    ReasionBeanDao reasionBeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getReasionBeanDao();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_index, container, false);
        binding.setFragment(this);
        return binding.getRoot();
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    protected void onVisible() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mList.clear();
        mList.add(new Info("", "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=15283720,130540256&fm=26&gp=0.jpg"));
        mList.add(new Info("", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1780545110,229391972&fm=26&gp=0.jpg"));
        mList.add(new Info("", "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1021814643,143183271&fm=26&gp=0.jpg"));
        mList.add(new Info("", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2346921421,2532606295&fm=26&gp=0.jpg"));
        //设置选中和未选中时的图片
        assert binding.cycleView != null;
        binding.cycleView.showContextMenu();
        binding.cycleView.setIndicators(R.drawable.module_draw_banner_select_true, R.drawable.module_draw_banner_select_false);
        //设置轮播间隔时间，默认为4000
        binding.cycleView.setDelay(1500);
        binding.cycleView.setData(mList, mAdCycleViewListener);

        rightStrs.clear();
        rightStrs.add("小学");
        rightStrs.add("初中");
        rightStrs.add("艺术类");
        rightStrs.add("体育");
        rightStrs.add("生活兴趣");
        TaoLinear taoLinear = new TaoLinear(getActivity(), 4);
        taoLinear.setScrollEnabled(false);
        binding.recyclerFunction.setLayoutManager(taoLinear);
        binding.recyclerFunction.setItemAnimator(new DefaultItemAnimator());
        rightAdapter = new FuncationAdapter(getActivity(), rightStrs);
        binding.recyclerFunction.setAdapter(rightAdapter);
        rightAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ToastUtil.showToast(rightStrs.get(position));
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });


        secondStrings.clear();
        secondStrings.add("数学");
        secondStrings.add("英语");
        secondStrings.add("语文");
        secondStrings.add("物理");
        secondStrings.add("化学");
        secondStrings.add("奥数");
        secondStrings.add("生物");
        secondStrings.add("钢琴");
        secondStrings.add("电子琴");
        secondStrings.add("古筝");
        TaoLinear taoLinear1 = new TaoLinear(getActivity(), 5);
        taoLinear1.setScrollEnabled(false);
        adapter = new ChildClassNameRightAdapter(getActivity(), secondStrings);
        binding.recyclerClassName.setLayoutManager(taoLinear1);
        binding.recyclerClassName.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                Intent intent = new Intent(getActivity(), MyInfoActivity.class);
                intent.putExtra("module_type","teacher" );
                intent.putExtra("module_class",secondStrings.get(pos));
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });


        /**
         * 数据库老师学生数据处理
         */

        //teacher
        ReasionBean teacher = new ReasionBean();
        teacher.setName("李帅丽");
        teacher.setSex("女");
        teacher.setAge("23");
        teacher.setEb("外国语研究生");
        teacher.setAddress("河南郑州市郑大西门");
        teacher.setJob("teacher");
        teacher.setClassName("英语");
        teacher.setTime("14:00-20:00");

        ReasionBean teacher1 = new ReasionBean();
        teacher1.setName("翟帅丽");
        teacher1.setSex("女");
        teacher1.setAge("28");
        teacher1.setEb("北大研究生");
        teacher1.setAddress("河南郑州市郑大东门");
        teacher1.setJob("teacher");
        teacher1.setClassName("数学");
        teacher1.setTime("14:00-20:00");

        ReasionBean teacher2 = new ReasionBean();
        teacher2.setName("孙冰");
        teacher2.setSex("女");
        teacher2.setAge("29");
        teacher2.setEb("郑大研究生");
        teacher2.setAddress("河南郑州市郑大东门");
        teacher2.setJob("teacher");
        teacher2.setClassName("语文");
        teacher2.setTime("14:00-20:00");


        ReasionBean teacher3 = new ReasionBean();
        teacher3.setName("赵燕");
        teacher3.setSex("女");
        teacher3.setAge("30");
        teacher3.setEb("食品系研究生");
        teacher3.setAddress("河南郑州市郑大东门");
        teacher3.setJob("teacher");
        teacher3.setClassName("物理");
        teacher3.setTime("14:00-20:00");

        ReasionBean teacher4 = new ReasionBean();
        teacher4.setName("王慧慧");
        teacher4.setSex("女");
        teacher4.setAge("26");
        teacher4.setEb("武汉理工研究生");
        teacher4.setAddress("河南郑州市郑大东门");
        teacher4.setJob("teacher");
        teacher4.setClassName("化学");
        teacher4.setTime("14:00-20:00");

        ReasionBean teacher5 = new ReasionBean();
        teacher5.setName("李玲");
        teacher5.setSex("女");
        teacher5.setAge("28");
        teacher5.setEb("外国语学院研究生");
        teacher5.setAddress("河南郑州市郑大东门");
        teacher5.setJob("teacher");
        teacher5.setClassName("奥数");
        teacher5.setTime("14:00-20:00");

        ReasionBean teacher6 = new ReasionBean();
        teacher6.setName("成都农大研究生");
        teacher6.setSex("女");
        teacher6.setAge("28");
        teacher6.setEb("食品系研究生");
        teacher6.setAddress("河南郑州市郑大东门");
        teacher6.setJob("teacher");
        teacher6.setClassName("生物");
        teacher6.setTime("14:00-20:00");


        ReasionBean teacher7 = new ReasionBean();
        teacher7.setName("王晓");
        teacher7.setSex("女");
        teacher7.setAge("28");
        teacher7.setEb("钢琴八级");
        teacher7.setAddress("河南郑州市郑大东门");
        teacher7.setJob("teacher");
        teacher7.setClassName("钢琴");
        teacher7.setTime("14:00-20:00");


        ReasionBean teacher8 = new ReasionBean();
        teacher8.setName("雪凡");
        teacher8.setSex("女");
        teacher8.setAge("28");
        teacher8.setEb("中央戏剧学院");
        teacher8.setAddress("河南郑州市郑大东门");
        teacher8.setJob("teacher");
        teacher8.setClassName("电子琴");
        teacher8.setTime("14:00-20:00");

        ReasionBean teacher9 = new ReasionBean();
        teacher9.setName("松下.美樱");
        teacher9.setSex("女");
        teacher9.setAge("28");
        teacher9.setEb("中央美术学院");
        teacher9.setAddress("河南郑州市郑大东门");
        teacher9.setJob("teacher");
        teacher9.setClassName("古筝");
        teacher9.setTime("14:00-20:00");

        //student
        ReasionBean student = new ReasionBean();
        student.setName("王哲");
        student.setAge("20");
        student.setSex("男");
        student.setAddress("河南省郑州市郑大北门");
        student.setJob("student");
        student.setClassName("数学");

        ReasionBean student1 = new ReasionBean();
        student1.setName("王雷");
        student1.setAge("20");
        student1.setSex("男");
        student1.setAddress("河南省郑州市郑大北门");
        student1.setJob("student");
        student1.setClassName("英语");

        ReasionBean student2 = new ReasionBean();
        student2.setName("宋莽");
        student2.setAge("20");
        student2.setSex("男");
        student2.setAddress("河南省郑州市郑大北门");
        student2.setJob("student");
        student2.setClassName("语文");

        ReasionBean student3 = new ReasionBean();
        student3.setName("吴智");
        student3.setAge("20");
        student3.setSex("男");
        student3.setAddress("河南省郑州市郑大北门");
        student3.setJob("student");
        student3.setClassName("物理");

        ReasionBean student4 = new ReasionBean();
        student4.setName("王力宏");
        student4.setAge("20");
        student4.setSex("男");
        student4.setAddress("河南省郑州市郑大北门");
        student4.setJob("student");
        student4.setClassName("化学");

        ReasionBean student5 = new ReasionBean();
        student5.setName("石达开");
        student5.setAge("20");
        student5.setSex("男");
        student5.setAddress("河南省郑州市郑大北门");
        student5.setJob("student");
        student5.setClassName("奥数");

        ReasionBean student6 = new ReasionBean();
        student6.setName("嬴政");
        student6.setAge("20");
        student6.setSex("男");
        student6.setAddress("河南省郑州市郑大北门");
        student6.setJob("student");
        student6.setClassName("电子琴");

        ReasionBean student7 = new ReasionBean();
        student7.setName("邓布利多");
        student7.setAge("20");
        student7.setSex("男");
        student7.setAddress("河南省郑州市郑大北门");
        student7.setJob("student");
        student7.setClassName("古筝");

        ReasionBean student8 = new ReasionBean();
        student8.setName("亚瑟");
        student8.setAge("20");
        student8.setSex("男");
        student8.setAddress("河南省郑州市郑大北门");
        student8.setJob("student");
        student8.setClassName("生物");

        if (reasionBeanDao.loadAll().size() == 0) {
            reasionBeanDao.insert(teacher);
            reasionBeanDao.insert(teacher1);
            reasionBeanDao.insert(teacher2);
            reasionBeanDao.insert(teacher3);
            reasionBeanDao.insert(teacher4);
            reasionBeanDao.insert(teacher5);
            reasionBeanDao.insert(teacher6);
            reasionBeanDao.insert(teacher7);
            reasionBeanDao.insert(teacher8);
            reasionBeanDao.insert(teacher9);

            reasionBeanDao.insert(student);
            reasionBeanDao.insert(student1);
            reasionBeanDao.insert(student2);
            reasionBeanDao.insert(student3);
            reasionBeanDao.insert(student4);
            reasionBeanDao.insert(student5);
            reasionBeanDao.insert(student6);
            reasionBeanDao.insert(student7);
            reasionBeanDao.insert(student8);
        }

        binding.tvModuleStudent.setOnClickListener(this);
        binding.tvModuleTeacher.setOnClickListener(this);

    }


    @Override
    protected void lazyLoad() {

    }

    /**
     * 得到轮播图的View
     *
     * @param context
     * @param url
     * @return
     */
    public static View getImageView(Context context, String url) {
        RelativeLayout rl = new RelativeLayout(context);
        ImageView imageView = new ImageView(context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(layoutParams);
        GlideUtil.setCircleMethod(context, url, imageView);
        ImageView backGround = new ImageView(context);
        backGround.setLayoutParams(layoutParams);
        backGround.setBackgroundResource(R.color.cycle_image_bg);
        rl.addView(imageView);
        rl.addView(backGround);
        return rl;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvModuleStudent:
                startActivity(new Intent(getActivity(), MyInfoActivity.class)
                        .putExtra("module_type", "student"));
                break;
            case R.id.tvModuleTeacher:
                startActivity(new Intent(getActivity(), MyInfoActivity.class)
                        .putExtra("module_type", "teacher"));
                break;
            default:
        }
    }


    int pointLocation = 0;
    private CycleViewPager.ImageCycleViewListener mAdCycleViewListener = new CycleViewPager.ImageCycleViewListener() {
        @Override
        public void onImageClick(Info info, int position, View imageView) {
            if (binding.cycleView.isCycle()) {
                pointLocation = position - 1;
            }
        }
    };


}
